package com.nbjtjc.safe.api.danger;

import com.nbjtjc.common.validator.ValidatorUtils;
import com.nbjtjc.common.validator.group.DeleteGroup;
import com.nbjtjc.common.validator.group.UpdateGroup;
import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.danger.DangerInspect;
import com.nbjtjc.safe.model.danger.DangerInspectList;
import com.nbjtjc.safe.model.danger.SafeDangerInspectLabel;
import com.nbjtjc.safe.model.sys.SysFile;
import com.nbjtjc.safe.service.danger.DangerInspectService;
import com.nbjtjc.safe.util.FileUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.validation.groups.Default;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/4/9 15:52
 * 隐患检查表的模板
 **/
@RestController
@RequestMapping("/danger/inspect")
@CrossOrigin("*")
public class DangerInspectController {

    @Autowired
    private DangerInspectService dangerInspectService;

    @Autowired
    private FileUtil fileUtil;

    @PostMapping("/collection")
    public Result collection(@ApiParam @RequestBody DangerInspect dangerInspect){
        ValidatorUtils.validateEntity(dangerInspect);
        Result result = new Result();
        result = dangerInspectService.collectionTable(dangerInspect);
        return result;
    }

    @SysLog("添加隐患排模板表")
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public Result add(@ApiParam @RequestBody DangerInspect dangerInspect) {
        ValidatorUtils.validateEntity(dangerInspect);
        Result result = new Result();
        result = dangerInspectService.add(dangerInspect);
        return result;
    }


    @SysLog("删除隐患排模板表")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public Result delete(@RequestBody DangerInspect dangerInspect) {
        ValidatorUtils.validateEntity(dangerInspect, DeleteGroup.class);
        Result result = new Result();
        result = dangerInspectService.delete(dangerInspect);
        return result;
    }

    @SysLog("更新隐患排模板表")
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result update(@RequestBody DangerInspect dangerInspect) {
        ValidatorUtils.validateEntity(dangerInspect, UpdateGroup.class);
        Result result = new Result();
        result = dangerInspectService.update(dangerInspect);
        return result;
    }

    @SysLog("查找检查表模板")
    @RequestMapping(value = "/find", method = {RequestMethod.POST})
    public Result find(@RequestBody SafeDangerInspectLabel safeDangerInspectLabel) {
        Result result = new Result();
        result = dangerInspectService.find("",safeDangerInspectLabel);
        return result;
    }

    @SysLog("导入检查表")
    @PostMapping(value = "/importDangerInspect")
    public Result importDangerInspect(HttpServletRequest request, DangerInspect dangerInspect) {
        Result result = new Result();
        try {
            // 解析器解析request的上下文未登录或者过期
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 先判断request中是否包含mutipart类型的数据
            if (multipartResolver.isMultipart(request)) {
                // 再将request中得数据转化成multipart类型的数据f
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
                Iterator iterator = multiRequest.getFileNames();
                while (iterator.hasNext()) {

                    MultipartFile file = multiRequest.getFile((String) iterator.next());
                    String filePath = fileUtil.getFilePath();
                    java.io.File targetFile = new java.io.File(filePath + file.getOriginalFilename());
                    if(!targetFile.getParentFile().exists()){
                        targetFile.getParentFile().mkdirs();
                    }
                    file.transferTo(targetFile);
                    result = dangerInspectService.importDangerInspect(targetFile, dangerInspect);
                }
            } else {
                result.setFalse("您未上传文件");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @SysLog("导入检查表word")
    @PostMapping(value = "importDangerInspectWord")
    public  Result importInspectWord(HttpServletRequest request, DangerInspect dangerInspect){
        Result result = new Result();
        try {
            // 解析器解析request的上下文未登录或者过期
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            // 先判断request中是否包含mutipart类型的数据
            if (multipartResolver.isMultipart(request)) {
                // 再将request中得数据转化成multipart类型的数据f
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                List<MultipartFile> multipartFiles = multiRequest.getFiles("file");
                Iterator iterator = multiRequest.getFileNames();
                while (iterator.hasNext()) {

                    MultipartFile file = multiRequest.getFile((String) iterator.next());
                    String filePath = fileUtil.getFilePath();
                    java.io.File targetFile = new java.io.File(filePath + file.getOriginalFilename());
                    if(!targetFile.getParentFile().exists()){
                        targetFile.getParentFile().mkdirs();
                    }
                    file.transferTo(targetFile);
                    result = dangerInspectService.importDangerInspectWord(targetFile, dangerInspect);
                }
            } else {
                result.setFalse("您未上传文件");
                return result;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @SysLog("查找检查表模板")
    @GetMapping(value = "/copyInspectTableByCompany/{fromCompanyId}/{toCompanyId}")
    public Result copyInspectTableByCompany(@PathVariable int fromCompanyId, @PathVariable int toCompanyId) {
        Result result = new Result();
        result.setData(dangerInspectService.copyInspectTableByCompany(fromCompanyId, toCompanyId));
        return result;
    }

    @GetMapping("/getSelfInspectId")
    public Result getSelfInspectId(){
        Result result=new Result();
        result=dangerInspectService.getSelfInspectId();
        return result;
    }
}
